package com.joelaws.prototype.client;

import com.google.gwt.ajaxsearch.client.ExpandMode;
import com.google.gwt.ajaxsearch.client.KeepLabel;
import com.google.gwt.ajaxsearch.client.LinkTarget;
import com.google.gwt.ajaxsearch.client.ResultSetSize;
import com.google.gwt.ajaxsearch.client.SearchControl;
import com.google.gwt.ajaxsearch.client.SearchControlOptions;
import com.google.gwt.ajaxsearch.client.WebSearch;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.rpc.Manage;
import com.joelaws.prototype.client.rpc.ManageAsync;

public class LoginPanel extends CenterPanel {

	public LoginPanel() {

		setLayout(new ColumnLayout());
		setBorder(false);
		setFrame(true);

		add(getLoginPanel(), new ColumnLayoutData(.4));

		add(getSearchPanel(), new ColumnLayoutData(.6));

	}

	public Panel getSearchPanel() {
		final Panel panel = new Panel();
		panel.setPaddings(10, 10, 0, 0);
		panel.setLayout(new AnchorLayout());
		panel.setBorder(false);
		panel.setFrame(false);
		panel.setAutoScroll(true);

		SearchControlOptions options = new SearchControlOptions();
		WebSearch ws = new WebSearch();
		ws.setResultSetSize(ResultSetSize.SMALL);
		options.add(ws, ExpandMode.OPEN);
		options.setKeepLabel(KeepLabel.SAVE);
		options.setLinkTarget(LinkTarget.BLANK);

		panel.add(new SearchControl(options));

		return panel;
	}

	public Panel getLoginPanel() {
		final FormPanel panel = new FormPanel();
		panel.setHeight(120);

		final TextField email = new TextField("Email", "email");
		email.setVtype(VType.EMAIL);
		panel.add(email, new AnchorLayoutData("100%"));

		final TextField password = new TextField("Password", "password");
		password.setPassword(true);
		panel.add(password, new AnchorLayoutData("100%"));

		Button save = new Button("Login", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				if (email.getText().equals("") || password.getText().equals("")) {
					return;
				}
				ManageAsync instance = Manage.Util.getInstance();
				instance.login(email.getText(), password.getText(), new AsyncCallback<User>() {

					public void onFailure(Throwable error) {
						Window.alert("Error occured:" + error.toString());
					}

					public void onSuccess(User user) {
						if (user == null) {
							System.out.println("Invalid username/password");
							Window.alert("Invalid username/password");
							return;
						}

						System.out.println("Logged in on client as " + user.getEmail());
						ScreenManager.instance().setLoggedIn(user);
					}
				});

			}
		});
		panel.addButton(save);

		return panel;
	}

	public String getName() {
		return "login";
	}

}
